package com.gexiaobao.pigeon.ui.fragment.mine.organization.activity

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.model.bean.AssignInfo
import com.gexiaobao.pigeon.app.model.bean.PassProjectInfo
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.ActivityParticipationPrizeBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterAppPassProject
import com.gexiaobao.pigeon.ui.dialog.RxDialogBonusDetail
import com.gexiaobao.pigeon.ui.dialog.RxDialogDetailInfo
import com.gexiaobao.pigeon.viewmodel.OrgRaceViewModel
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2024/7/22 14:42
 * @Describe    : 参赛奖
 */
class ActivityParticipationPrize : BaseActivity<OrgRaceViewModel, ActivityParticipationPrizeBinding>() {

    private var projectId = 0
    private val mAdapter: AdapterAppPassProject by lazy { AdapterAppPassProject(arrayListOf()) }
    private var dataBonus: ArrayList<AssignInfo> = arrayListOf()

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        val title = intent.getStringExtra("titleGroup")
        mDatabind.includeBar.tvTitle.text = title

        projectId = intent.getIntExtra("projectId", 0)

        mDatabind.includeList.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter)

        onRefresh()

        mDatabind.includeList.swipeRefreshLayout.init {
            onRefresh()
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            showInfoDetailDialog(mAdapter.data[position])
        }
    }

    private fun showInfoDetailDialog(data: PassProjectInfo) {
        val dialog = RxDialogDetailInfo(this)
        dialog.setDataAppPass(data)
        dialog.setSureListener {
            dialog.dismiss()
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }

    private fun onRefresh() {
        showLoading(getString(R.string.loading))
        mViewModel.getAppPassProject(projectId.toString())
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        mViewModel.appProjectResult.observe(this) {
            if (mDatabind.includeList.swipeRefreshLayout.isRefreshing) {
                mDatabind.includeList.swipeRefreshLayout.isRefreshing = false
            }
            mAdapter.setList(it.passProjectInfo)
            mAdapter.notifyDataSetChanged()

            mDatabind.tvBonusDetail.isVisible = !it.assignInfos.isNullOrEmpty()
            mDatabind.tvMaxRank.text = if (it.maxProjectRank == 0) "--" else it.maxProjectRank.toString()

            if (!it.assignInfos.isNullOrEmpty()) {
                dataBonus.clear()
                dataBonus.addAll(it.assignInfos)
            }
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack, mDatabind.tvBonusDetail) {
            when (it) {
                mDatabind.tvBonusDetail -> {
                    showBonusDetailDialog()
                }

                mDatabind.includeBar.ivBack -> {
                    finish()
                }
            }
        }
    }

    private fun showBonusDetailDialog() {
        val dialog = RxDialogBonusDetail(this, dataBonus)
        dialog.setSureListener {
            dialog.dismiss()
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }
}